package com.baizhi.cmfz.realm;

import com.baizhi.cmfz.dao.UserDao;
import com.baizhi.cmfz.entity.User;
import org.apache.shiro.authc.*;
import org.apache.shiro.realm.AuthenticatingRealm;
import org.springframework.beans.factory.annotation.Autowired;


public class AuthenRealm extends AuthenticatingRealm {
   @Autowired
    private UserDao userDao;

    /**
     * 查询数据库 用户输入的数据给数据库数据做比对
     * @param authenticationToken
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        //获取Token中的数据
        UsernamePasswordToken token1= (UsernamePasswordToken) token;
        //拿到用户账号
        String username = token1.getUsername();
        //2.根据用户名查询数据库
        User user = userDao.loginUser(username);
        System.out.println(user+"auth");
        if(user != null){
            String password = user.getPassword();
            System.out.println(password);
            //如果user != null 并且比较密码为真 封装info
            System.out.println(token1.getPassword()+"token");
//            char[] password1 = token1.getPassword();
//            String password3= String.valueOf(password1);
//            System.out.println(password3+"转换后");
             return  new SimpleAuthenticationInfo(user.getUserName(),user.getPassword(),this.getName());
//            if(password.equals(password3)){
//                System.out.println("密码通过");
//
//                //参数1 数据库中查询到的账号 参数2 密码 参数3当前对象类名
//
//            } System.out.println("user="+user);
        }

        return null;
    }
}
